package com.example.threemoni.controller;

import com.example.threemoni.pojo.Goods;
import com.example.threemoni.pojo.Type;
import com.example.threemoni.pojo.vo.GoodsVo;
import com.example.threemoni.service.IGoodsService;
import com.example.threemoni.util.R;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/10/25
 * @Version 1.0
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;

    @GetMapping("/types")
    public R findTypes(){
        List<Type> types = this.goodsService.types();
        return  types!=null
                ? new R(200,true,"查询成功",types)
                : new R(201,false,"查询失败");
    }

    @GetMapping("/")
    public R findAll(@RequestParam(name = "tid",required = false)Integer tid,
                     @RequestParam(name="offset",defaultValue = "1")Integer offset,
                     @RequestParam(name="pagesize",defaultValue = "3")Integer pagesize){
        PageInfo<GoodsVo> pager = this.goodsService.findGoods(offset,pagesize,tid);
        return  pager!=null
                ? new R(200,true,"查询成功",pager)
                : new R(201,false,"查询失败");
    }

    @PostMapping("/")
    public R saveGoods(@RequestBody Goods goods){

        return  this.goodsService.save(goods)
                ? new R(200,true,"新增成功")
                : new R(201,false,"新增失败");
    }


    @GetMapping("/{gname}")
    public R hasName(@PathVariable String  gname){
        return  this.goodsService.hasName(gname)
                ? new R(200,true,"可以使用")
                : new R(201,false,"商品名称已存在");
    }

    @DeleteMapping("/{ids}")
    public R delete(@PathVariable String ids){
        return  this.goodsService.delete(ids)
                ? new R(200,true,"删除成功")
                : new R(201,false,"删除失败！");
    }
}
